package com.sx.gongzhong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.model.LegalAdviceTypeBean;
import com.sx.gongzhong.model.MsgEvent;
import com.sx.gongzhong.net.MyRxBus;
import com.sx.gongzhong.pest.LegalAdviceAddPest;
import com.sx.gongzhong.utils.BaseUtil;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.IVoidView;
import com.sx.gongzhong.widget.CountEditText;
import com.sx.gongzhong.widget.LoadDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新增法律咨询
 */
public class LegalAdviceAddActivity extends BaseActivity implements IVoidView {

    @BindView(R.id.tv_Type)
    TextView tv_Type;
    @BindView(R.id.et_Content)
    CountEditText et_Content;
    @BindView(R.id.tv_count)
    TextView tv_count;

    String content;
    private String advisoryTypeId;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_legal_advice_add);
        setTitleName(R.string.legal_advice_add);
        setTitleRight(R.string.submit);
        ButterKnife.bind(this);
        initViews();
    }

    @OnClick({R.id.tv_Type,R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_Type:
                startActivityForResult(new Intent(LegalAdviceAddActivity.this,LegalTypeSelectActivity.class),110);
                break;
            case R.id.tv_right:
                if (BaseUtil.isEmpty(advisoryTypeId)){
                    ToastUtils.showToast(LegalAdviceAddActivity.this,"请选择咨询类型");
                    return;
                }
                content = et_Content.getText().toString().trim();
                if (BaseUtil.isEmpty(content)){
                    ToastUtils.showToast(LegalAdviceAddActivity.this,"请输入咨询内容");
                    return;
                }
                LoadDialog.show(LegalAdviceAddActivity.this,"提交中..");
                new LegalAdviceAddPest(this).addLegalAdvice(advisoryTypeId,content);
                break;
        }
    }

    private void initViews() {
        et_Content.setMaxNum(500);
        et_Content.setOnEditTextCountLisener(new CountEditText.OnEditTextCountLisener() {
            @Override
            public void setCount(int currentCount) {
                tv_count.setText(currentCount+"/500");
            }
        });

    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(LegalAdviceAddActivity.this);
        ToastUtils.showToast(LegalAdviceAddActivity.this,message);
    }

    @Override
    public void voidView(String message) {
        LoadDialog.dismiss(LegalAdviceAddActivity.this);
        ToastUtils.showToast(LegalAdviceAddActivity.this,message);
        MyRxBus.get().post(new MsgEvent("LegalAdviceActivity",1,null));
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            if (requestCode == 110){
                LegalAdviceTypeBean bean = (LegalAdviceTypeBean) data.getSerializableExtra("typesBean");
                tv_Type.setText(bean.getName());
                advisoryTypeId = bean.getId();
            }
        }
    }
}
